package com.emep.changzhi.analyse.service.Technology;

import com.emep.changzhi.analyse.entity.Technology.SCRDenitrification;
import com.emep.changzhi.analyse.entity.Technology.SNCRDenitrification;
import com.emep.changzhi.analyse.entity.Technology.SemiDryDesulfurization;
import com.emep.changzhi.analyse.repository.Technology.SemiDryDesulfurizationRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 半干法脱硫
 */
@Slf4j
@Service
public class SemiDryDesulfurizationService {

    @Resource
    private SemiDryDesulfurizationRepository semiDryDesulfurizationRepository;

    /**
     * 添加
     * @param payload
     * @return
     */

    public SemiDryDesulfurization add(Map<String, Object> payload) {
        SemiDryDesulfurization SemiDryDesulfurization = null;
        try {


            SemiDryDesulfurization semiDryDesulfurization1 = (SemiDryDesulfurization) MapUtils.convertMap(SemiDryDesulfurization.class,payload);
            SemiDryDesulfurization = semiDryDesulfurizationRepository.save(semiDryDesulfurization1);
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return SemiDryDesulfurization;
    }

    /**
     * 根据时间查询
     * @param page
     * @param rows
     * @param payload
     * @return
     */
    public Page<SemiDryDesulfurization> list(Integer page, Integer rows, Map<String, Object> payload) {
        //规格定义
        Pageable pageable = new PageRequest(page-1,rows , Sort.Direction.ASC, "saveDate"); //页码：前端从1开始，jpa从0开始，做个转换
        Page<SemiDryDesulfurization> studentPage = semiDryDesulfurizationRepository.findAll(new Specification<SemiDryDesulfurization>(){
            @Override
            public Predicate toPredicate(Root<SemiDryDesulfurization> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                if(!payload.isEmpty()){
                    if(payload.get("begin") != null && payload.get("end") != null){
                        Predicate p1 = criteriaBuilder.between(root.get("saveDate").as(String.class),payload.get("starTime").toString(),payload.get("endTime").toString());
                        query.where(criteriaBuilder.or(p1));
                    }
                }
                return query.getRestriction();
            }
        },pageable);
        return studentPage;
    }
}
